package com.itheima.dao.impl;

import com.itheima.dao.BrandMapper;
import com.itheima.bean.Brand;
import com.itheima.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

//mybatis相关的代码写到dao层(mapper层)
public class BrandMapperImpl implements BrandMapper {

    @Override
    public List<Brand> selectAll() {
        SqlSession session = MyBatisUtils.getSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        //访问完毕  关闭会话
        session.close();
        return brands;
    }

    @Override//根据id查品牌
    public Brand selectBrandById(Integer id) {
        SqlSession session = MyBatisUtils.getSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        Brand brand = mapper.selectBrandById(id);
        //访问完毕  关闭会话
        session.close();
        return brand;
    }

    @Override
    public void add(Brand brand) {
        //获得自动提交事务的会话
        SqlSession session = MyBatisUtils.getAutoSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        mapper.add(brand);
        //访问完毕  关闭会话
        session.close();
    }

    @Override
    public void deleteBrand(Integer id) {
        //获得自动提交事务的会话
        SqlSession session = MyBatisUtils.getAutoSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        mapper.deleteBrand(id);
        //访问完毕  关闭会话
        session.close();
    }

    @Override
    public void upDate(Brand brand) {
        //获得自动提交事务的会话
        SqlSession session = MyBatisUtils.getAutoSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        mapper.upDate(brand);
        //访问完毕  关闭会话
        session.close();
    }
}
